package app.netant.book.jiaodui8;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class Jiaodui8Detail extends BaseDetail {

	public Jiaodui8Detail(String netSite) {
		super(netSite);
		this.preHref = "http://www.jiaodui8.com";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select("#kuangjia .neirong .reed .reed-biaoti2 .reed-biaoti h1");
		if(parts.size()>0){
			String tmp = parts.get(0).text();
			int st = tmp.indexOf("《");
			int ed = tmp.indexOf("》");
			if(st>=0 && ed>=0 && ed>st){
				String title = tmp.substring(st+1,ed).trim();
				System.out.println("【Title】:"+title);
				this.entity.setNovelName(title);
				this.entity.setNovelFullName(title+".rar");
			}
			int idx = tmp.indexOf("作者：");
			if(idx>0){
				String writer = tmp.substring(idx+3).trim();
				System.out.println("【Writer】:"+writer);
				this.entity.setNovelWriter(writer);
			}
		}
	}

	@Override
	public void analyseTags() {
		
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select("#kuangjia .neirong .reed dd.kan p");
		for(int i=0;i<parts.size();i++){
			String text = parts.get(i).text().trim();
			if(text.indexOf("简介")>=0){
				int idx = i +1;
				if(idx<parts.size()){
					String memo = this.getString(parts.get(idx).text().trim());
					System.out.println("【Memo】："+memo);
					this.entity.setNovelMemo(memo);
				}
			}
		}
	}

	@Override
	public void analyseHref() {
		Elements parts = this.select("#kuangjia .neirong .reed dd.kan .pagefujian .down_2 a");
		if(parts.size()>0){
			String refer = parts.get(0).attr("href");
			System.out.println("【Refer】:"+refer);
			this.entity.setRefer(refer);
			
			Jiaodui8Detail detail = new Jiaodui8Detail(refer);
			String href = detail.stepHref();
			System.out.println("【Href】:"+href);
			this.entity.setDownPath(href);
			
			int idx = href.lastIndexOf(".");
			String exp = href.substring(idx).trim();
			System.out.println("【Exp】:"+exp);
			String title = this.entity.getNovelName();
			this.entity.setNovelFullName(title+exp);
			
		}
	}
	
	public String stepHref(){
		
		Elements parts = this.select(".wrap .content .panel #lanyedownfile .downfile a");
		if(parts.size()>0){
			return parts.get(0).attr("href");
		}
		
		return null;
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select("#kuangjia .neirong .reedd #shuomingg h3 a");
		if(parts.size()>0){
			String type = parts.get(0).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		Jiaodui8Detail detail = new Jiaodui8Detail("http://www.jiaodui8.com/fiction/10.html");
		detail.test();
	}



}
